﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Winner.Game.Common;
using Winner.Game.Entity.DTOModel;
using Winner.Game.Entity.POCOModel;
using Winner.Game.Entity.ViewModel;

namespace Winner.Game.Services
{
    //实现会员增删改查
    public class UserInfoServices : BaseServices<UserInfo>
    {

        /// <summary>
        /// 对会员页表进行分页
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public OperateResult UserInfoList(UserInfoSearchViewModel viewModel)
        {
            int rowCount = 0;

            //多条件查询，条件拼接
            //利用lambda表达式返回一个IQueryable的接口
            var query = PredicateExtensions.True<UserInfo>();

            if (!string.IsNullOrWhiteSpace(viewModel.UserID))
                query = query.And(m => m.UserID.ToString() == viewModel.UserID);

            if (!string.IsNullOrWhiteSpace(viewModel.UserName))
                query = query.And(m => m.UserName.Contains(viewModel.UserName));

            if (!string.IsNullOrWhiteSpace(viewModel.UserPhone))
                query = query.And(m => m.UserPhone.ToString() == viewModel.UserPhone);

            if (viewModel.UserSex > 0)
                query = query.And(m => m.UserSex == viewModel.UserSex);

            //根据下拉框选中的值判断，如果大于0说明选中的不是全部
            if (viewModel.UserLevel > 0)
                query = query.And(m => m.UserLevel == viewModel.UserLevel);

            //调用BaseService封装好的分页方法
            var list = GetPageList(viewModel.Page, viewModel.Rows, ref rowCount, query, m => m.ID, false)
                .Select(m => new UserInfoViewModel()
                {
                    ID = m.ID,
                    UserID = m.UserID,
                    UserName = m.UserName,
                    LoginName = m.LoginName,
                    UserPassword = m.UserPassword,
                    UserSex = ((SexEnum)m.UserSex).ToString(),
                    UserPhone = m.UserPhone,
                    UserEmail = m.UserEmail,
                    UserLevel = m.UserLevel1.LevelName,
                    UserPoint = m.UserPoint,
                    UserState = ((StateEnum)m.UserState).ToString(),
                    UserCreateTime = (DateTime)m.UserCreateTime
                });

            var data = new EasyUIDataGrid() { total = rowCount, rows = list };

            return new OperateResult(ResultState.Success, "", data);

        }


        /// <summary>
        /// 添加用户的业务
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public OperateResult AddUserInfo(UserInfoEditViewModel viewModel)
        {
            //实例化出POCO模型
            var model = new UserInfo()
            {
                UserID = viewModel.UserID,
                UserName = viewModel.UserName,
                LoginName = viewModel.LoginName,
                UserPassword = viewModel.UserPassword,
                UserSex = (int)viewModel.UserSex,
                UserPhone = viewModel.UserPhone,
                UserEmail = viewModel.UserEmail,
                UserLevel = viewModel.UserLevel,
                UserPoint = viewModel.UserPoint,
                UserState = (int)viewModel.UserState,
                UserCreateTime = DateTime.Now,

            };

            //调用父类中公共的添加方法
            if (Add(model))
            {
                return new OperateResult(ResultState.Success, "氪金之路，现在开启！！（♥添加成功♥）");
            }
            else
            {
                return new OperateResult(ResultState.Error, "网络问题，请稍后重试...");
            };
        }

        /// <summary>
        /// 根据Id查询用户实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OperateResult GetUserInfoID(int id)
        {
            var model = Find(e => e.ID == id);
            if (model != null)
            {
                var viewModel = new UserInfoEditViewModel()
                {
                    UserID = model.UserID,
                    UserName = model.UserName,
                    LoginName = model.LoginName,
                    //UserPassword = model.UserPassword.ToString(),
                    UserSex = (SexEnum)model.UserSex,
                    UserPhone = model.UserPhone,
                    UserEmail = model.UserEmail,
                    UserLevel = model.UserLevel,
                    UserPoint = model.UserPoint,
                    UserState = (StateEnum)model.UserState
                };

                return new OperateResult(ResultState.Success, "", viewModel);
            }
            else
            {
                return new OperateResult(ResultState.Error, "该会员未找到！Sorry<QuQ>");
            };
        }

        /// <summary>
        /// 更新用户信息的业务
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        public OperateResult EditUserInfo(UserInfoEditViewModel viewModel)
        {
            //调用父类里面封装的公共查询方法
            var model = Find(e => e.ID == viewModel.ID);
            if (model != null)
            {
                model.UserID = viewModel.UserID;
                model.UserName = viewModel.UserName;
                model.LoginName = viewModel.LoginName;
                //model.U_Password = viewModel.U_Password;
                model.UserSex = Convert.ToInt32(viewModel.UserSex);
                model.UserPhone = viewModel.UserPhone;
                model.UserEmail = viewModel.UserEmail;
                model.UserLevel = Convert.ToInt32(viewModel.UserLevel);
                model.UserPoint = viewModel.UserPoint;
                model.UserState = Convert.ToInt32(viewModel.UserState);

                if (Update(model))
                {
                    return new OperateResult(ResultState.Success, "巴啦啦！小魔仙全身变！（♥成功咯♥）");
                }
                else
                {
                    return new OperateResult(ResultState.Error, "网络问题，请稍后重试...");
                }
            }
            else
            {
                return new OperateResult(ResultState.Error, "该会员不存在");
            }

        }

        /// <summary>
        /// 删除用户的业务
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OperateResult DeleteUserInfo(int id)
        {
            var model = Find(e => e.ID == id);
            if (model != null)
            {
                if (Delete(model))
                {
                    return new OperateResult(ResultState.Success, "跟着他的小姨子，跑路了！！！");
                }
                else
                {
                    return new OperateResult(ResultState.Error, "网络问题，请稍后重试...");
                }
            }
            else
            {
                return new OperateResult(ResultState.Error, "该学员已经被删除，请刷新后重试~~~");
            }
        }
    }
}

